package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmcc.aregister.MyApplication;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.model.Department;

public class DepartmentAdapter extends BaseAdapter
{
    private Context context = null;
    public List<Department> list = null;
    private Handler handler;
    public int currPage;
    public int allPage;

    public DepartmentAdapter(Context context, List<Department> list,
        Handler handler, int currPage, int allPage, boolean cancelBtn)
    {
        this.context = context;
        this.list = list;
        this.handler = handler;
        this.currPage = currPage;
        this.allPage = allPage;

    }

    public int getCount()
    {
        int size = list.size();
        if (allPage == 0)
        {
            allPage = 1;
        }
        if (currPage == 0)
        {
            currPage = 1;
        }
        if (allPage > currPage)
        {
            return size + 1;
        }
        else
        {
            return size;
        }
    }

    @Override
    public boolean isEnabled(int position)
    {
        if (position >= list.size())
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public Object getItem(int position)
    {
        return position;
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (position >= list.size())
        {
            handler.sendMessage(handler.obtainMessage(-1, position));
            convertView = LayoutInflater.from(context).inflate(
                R.layout.list_wait, null);
            convertView.setTag(null);
            convertView.setEnabled(false);
            return convertView;
        }
        else
        {
            ViewHolder holder;
            if (convertView != null && convertView.getTag() != null)
            {
                holder = (ViewHolder) convertView.getTag();
            }
            else
            {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                    R.layout.a_department, null);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.address = (TextView) convertView
                    .findViewById(R.id.address);
                holder.level = (TextView) convertView.findViewById(R.id.level);
                convertView.setTag(holder);
            }

            Department department = list.get(position);
            holder.name.setText(department.dep_name);
            holder.address.setText(department.unit_name);
            holder.level.setText(department.key_level);

            holder.attention_layout = (LinearLayout) convertView
                .findViewById(R.id.attention_layout);
            String is_attention = MyApplication.getInstance().hospitalMap
                .get(department.dep_id);
            if (is_attention == null || "".equals(is_attention))
            {
                is_attention = department.is_attention;
            }
            if ("1".equals(is_attention))
            {
                holder.attention_layout.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.attention_layout.setVisibility(View.GONE);
            }

            return convertView;
        }
    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            handler.sendMessage(handler.obtainMessage(1, v.getTag()));
        }
    };

    static class ViewHolder
    {
        TextView name;
        TextView address;
        TextView level;
        LinearLayout attention_layout;
    }
}